"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '11/05/2025'
    comment: 智能翻译
"""
from fastapi import APIRouter, Request, Body

from project.model.translation import ApiTranslate, OutputFormat, ApiConversation
from project.api import api_handler
from project.service.translation import TranslationService


translation_v1_router = APIRouter(prefix='/api/v1', tags=['V1'])


@translation_v1_router.post('/translate', summary='创建房间', response_model=ApiTranslate)
@api_handler()
async def translate(request: Request,
                    text: str = Body(..., title='翻译的文本'),
                    output_format: OutputFormat = Body('word', title='输出格式', description='word(含word文档)/json(不含word文档)'),
                    include_vocabulary: bool = Body(True, title='是否包含词汇列表')):
    """
    **desc**: 智能翻译

    **url**: /api/v1/translate

    **method**: POST

    **params**:

        text: 要翻译的文本
        output_format: 输出格式 word(含word文档)/json(不含word文档)
        include_vocabulary: 是否包含词汇列表

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:{
                translation: 翻译后文本
                vocabulary: [
                    {
                        english: 英语翻译
                        chinese: 中文翻译
                        explanation: 词汇解释
                    }
                    ...
                ] 专业词汇列表
            }
        }
    """
    data = await TranslationService.translate(text, output_format, include_vocabulary)

    return ApiTranslate.success(data=data)


@translation_v1_router.get('/translate/conversations', summary='AI对话记录(智能翻译)', response_model=ApiConversation)
@api_handler()
async def conversations(request: Request):
    """
    **desc**: 智能翻译

    **url**: /api/v1/conversations

    **method**: GET

    **params**:

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                conversations: [
                    {
                        message: 用户话术
                        reply: 模型回复
                        duration: 推理时长 单位: 秒
                    }
                    ...
                ]
            }
        }
    """
    data = TranslationService.get_conversations()

    return ApiConversation.success(data={'conversations': data})
